Java syntax
part 18/23 Β· 86.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
WINTER {
String getDescription() {return "cold";}
},
SPRING {
String getDescription() {return "warmer";}
},
SUMMER {
String getDescription() {return "hot";}
},
FALL {
String getDescription() {return "cooler";}
};
}
Interfaces
Interfaces are types which contain no fields and usually define a number
of methods without an actual implementation. They are useful to define a
contract with any number of different implementations. Every interface
is implicitly abstract. Interface methods are allowed to have a subset
of access modifiers depending on the language version, strictfp, which
has the same effect as for classes, and also static since Java SE 8.
interface ActionListener {
int ACTION_ADD = 0;
int ACTION_REMOVE = 1;
void actionSelected(int action);
}
Implementing an interface
An interface is implemented by a class using the implements keyword. It
is allowed to implement more than one interface, in which case they are
written after implements keyword in a comma-separated list. A class
implementing an interface must override all its methods, otherwise it
must be declared as abstract.
interface RequestListener {
int requestReceived();
}
class ActionHandler implements ActionListener, RequestListener {
public void actionSelected(int action) {
}
public int requestReceived() {
}
}
//Calling method defined by interface
RequestListener listener = new ActionHandler(); /*ActionHandler can be
represented as RequestListener...*/
listener.requestReceived(); /*...and thus is known to implement
requestReceived() method*/
Functional interfaces and lambda expressions
These features were introduced with the release of Java SE 8. An
interface automatically becomes a functional interface if it defines
only one method. In this case an implementation can be represented as a
lambda expression instead of implementing it in a new class, thus
greatly simplifying writing code in the functional style. Functional
interfaces can optionally be annotated with the @FunctionalInterface
annotation, which will tell the compiler to check whether the interface
actually conforms to a definition of a functional interface.
// A functional interface
@FunctionalInterface
interface Calculation {
int calculate(int someNumber, int someOtherNumber);
}
// A method which accepts this interface as a parameter
int runCalculation(Calculation calculation) {
return calculation.calculate(1, 2);
}
// Using a lambda to call the method
runCalculation((number, otherNumber) -> number + otherNumber);
// Equivalent code which uses an anonymous class instead
runCalculation(new Calculation() {
@Override
public int calculate(int someNumber, int someOtherNumber) {
return someNumber + someOtherNumber;
}
})
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ